package expression.v1.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ResourceUtils;

@Configuration
//@ConfigurationProperties(prefix="db")
public class DataSourceConfig {

	@Bean(name="jdbcTemplate")
	public JdbcTemplate jdbcTemplate4Mysql() throws IOException {
		File file = ResourceUtils.getFile("classpath:datasource.properties");
		Properties pro = new Properties();
		pro.load(new FileInputStream(file));
		String driver = pro.getProperty("db.driverClassName");
		String url = pro.getProperty("db.url");
		String user = pro.getProperty("db.user");
		String password = pro.getProperty("db.password");
		
		DataSource ds = DataSourceBuilder.create(this.getClass().getClassLoader())
				.driverClassName(driver)
				.url(url)
				.username(user)
				.password(password)
				.build();
		return new JdbcTemplate(ds);
	}
}
